package com.ruoyi.vehicle.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.constant.VehicleConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.vehicle.domain.TVehicleInfo;
import com.ruoyi.vehicle.service.ITVehicleInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车辆信息Controller
 * 
 * @author wzy
 * @date 2024-10-12 20:41:53
 */
@RestController
@RequestMapping("/vehicles/vehicle")
public class TVehicleInfoController extends BaseController
{
    @Autowired
    private ITVehicleInfoService tVehicleInfoService;

    /**
     * 查询车辆信息列表
     */
    @PreAuthorize("@ss.hasAnyPermi('vehicles:vehicle:list, vehicles:intervehicle:list, vehicles:extervehicle:list')")
    @GetMapping("/list")
    public TableDataInfo list(TVehicleInfo tVehicleInfo)
    {
        startPage();
        List<TVehicleInfo> list = tVehicleInfoService.selectTVehicleInfoList(tVehicleInfo);
        return getDataTable(list);
    }

    /**
     * 导出车辆信息列表
     */
    @PreAuthorize("@ss.hasAnyPermi('vehicles:vehicle:export, vehicles:intervehicle:export, vehicles:extervehicle:export')")
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TVehicleInfo tVehicleInfo)
    {
        List<TVehicleInfo> list = tVehicleInfoService.selectTVehicleInfoList(tVehicleInfo);
        ExcelUtil<TVehicleInfo> util = new ExcelUtil<TVehicleInfo>(TVehicleInfo.class);
        util.exportExcel(response, list, "车辆信息数据");
    }

    /**
     * 获取车辆信息详细信息
     */
    @PreAuthorize("@ss.hasAnyPermi('vehicles:vehicle:query, vehicles:intervehicle:query, vehicles:extervehicle:query')")
    @GetMapping(value = "/{vehicleId}")
    public AjaxResult getInfo(@PathVariable("vehicleId") Long vehicleId)
    {
        return success(tVehicleInfoService.selectTVehicleInfoByVehicleId(vehicleId));
    }

    /**
     * 新增车辆信息
     */
    @PreAuthorize("@ss.hasAnyPermi('vehicles:vehicle:add, vehicles:intervehicle:add, vehicles:extervehicle:add')")
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TVehicleInfo tVehicleInfo)
    {
        if (VehicleConstants.NOT_UNIQUE.equals(tVehicleInfoService.checkVehicleLicenseNumberUnique(tVehicleInfo)))
        {
            return error("新增车辆'" + tVehicleInfo.getVehicleLicenseNumber() + "'失败，车牌号码已存在");
        }
        tVehicleInfo.setCreateBy(getNickName());
        return toAjax(tVehicleInfoService.insertTVehicleInfo(tVehicleInfo));
    }

    /**
     * 修改车辆信息
     */
    @PreAuthorize("@ss.hasAnyPermi('vehicles:vehicle:edit, vehicles:intervehicle:edit, vehicles:extervehicle:edit')")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TVehicleInfo tVehicleInfo)
    {
        if (VehicleConstants.NOT_UNIQUE.equals(tVehicleInfoService.checkVehicleLicenseNumberUnique(tVehicleInfo)))
        {
            return error("修改车辆'" + tVehicleInfo.getVehicleLicenseNumber() + "'失败，车牌号码已存在");
        }
        tVehicleInfo.setUpdateBy(getNickName());
        return toAjax(tVehicleInfoService.updateTVehicleInfo(tVehicleInfo));
    }

    /**
     * 删除车辆信息
     */
    @PreAuthorize("@ss.hasAnyPermi('vehicles:vehicle:remove, vehicles:intervehicle:remove, vehicles:extervehicle:remove')")
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{vehicleIds}")
    public AjaxResult remove(@PathVariable Long[] vehicleIds)
    {
        return toAjax(tVehicleInfoService.deleteTVehicleInfoByVehicleIds(vehicleIds));
    }

    /**
     * 查询车辆信息选项
     */
//    @PreAuthorize("@ss.hasAnyPermi('vehicles:vehicle:list, vehicles:intervehicle:list, vehicles:extervehicle:list')")
    @GetMapping("/listoption")
    public AjaxResult listOption(TVehicleInfo tVehicleInfo)
    {
        return success(tVehicleInfoService.selectTVehicleInfoAll(tVehicleInfo));
    }
}
